package com.apes.fico.engine.model;


import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fico.masterdata.gl.model.AccountingModel;
import com.apes.fico.masterdata.gl.model.AccountingVoucherType;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ReduceVoucher.CONST_TABLE_NAME,
        indexes = {
                @Index(columnList = "cycle,cycle_value,company_id,acc_voucher_type_id,remark,voucher_template_id", unique = true)
        })
public class ReduceVoucher extends Model {
    public final static String CONST_TABLE_NAME = "gl_voucher_reduce";

    @Id
    @JSONField(serialize = false)
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "归约财务凭证", padding = 8)
    @Comment("编码")
    private String id;


    @Column(name = "cycle", length = 10, nullable = false)
    @Comment("归约周期")
    private String cycle; // 按天、月归约

    public void checkCycle(String cycle) {
        if(!AccountingModel.D.equals(cycle) && !AccountingModel.M.equals(cycle)) {
            throw new RuntimeException("归约周期必须是天或月！");
        }
    }


    @Column(name = "cycle_value", length = 8, nullable = false)
    @Comment("归约值")
    private String cycleValue; // yyyyMM || yyyyMMdd


    @NotNull
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "post_date")
    @Comment("记账日期")
    private Date postDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "acc_voucher_type_id", referencedColumnName = "id", nullable = false)
    @Comment("凭证类型")
    private AccountingVoucherType accountingVoucherType;

    @OneToMany(mappedBy = "voucher", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ReduceVoucherItem> voucherItemList = new ArrayList(); //凭证分录

    public List<ReduceVoucherItem> getVoucherItemList() {
        for (ReduceVoucherItem item : voucherItemList) {
            item.setVoucher(this);
        }
        return voucherItemList;
    }

    @Column(name = "debit_total", columnDefinition = ApesConst.FIELD_MONEY_16_4_0, nullable = false)
    @Comment("借方合计")
    private double debitTotal = 0;

    public void changeDebitTotal(double debitTotal) {
        this.debitTotal += debitTotal;
    }

    @Column(name = "credit_total", columnDefinition = ApesConst.FIELD_MONEY_16_4_0, nullable = false)
    @Comment("贷方合计")
    private double creditTotal = 0;

    public void changeCreditTotal(double creditTotal) {
        this.creditTotal += creditTotal;
    }


    @Column(name = "remark", length = 200, nullable = false)
    @Comment("备注")
    private String remark;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "voucher_template_id", referencedColumnName = "id", nullable = false)
    @Comment("模板类型")
    private VoucherTemplate voucherTemplate;

    @Column(name = "hava_error",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE, nullable = false)
    @Comment("有错误")
    private Boolean haveError = false;


    @Column(name = "done",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE, nullable = false)
    @Comment("已完成标记")
    private Boolean done = false;


    @Column(name = "voucher_id", length = 120)
    @Comment("凭证号码")
    private String voucherId;

}
